package cn.edu.djtu.fw.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.model.Lost;
import cn.edu.djtu.fw.service.LostServiceI;
import cn.edu.djtu.fw.service.SystemServiceI;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LostAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private LostServiceI lostService;
	private SystemServiceI systemService;
	private Lost lost;
	private int page;
	private int rows;
	private String ids;
	private File image; // 上传的文件
	private String imageFileName; // 文件名称
	private String imageContentType; // 文件类型

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = lostService.getAllLost(page, rows, lost);
		JsonUtil.writeJson(m);
		return null;
	}

	@SuppressWarnings("unchecked")
	public String i_show() throws Exception {
		HttpSession session = request.getSession();
		if (lost != null) {
			session.setAttribute("s_lost", (List<Lost>) lostService.getAllLost(1, 8, lost).get("rows"));
		}
		return "lost_show";
	}

	public void add() throws Exception {
		Json j = new Json();
		try {
			fileUpload("images/lost", image);
			lost.setImgurl("images/lost/" + imageFileName);
			if (lostService.insert(lost)) {
				j.setSuccess(true);
				j.setMsg("添加成功！");
			} else {
				j.setMsg("添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		JsonUtil.writeJson(j);
	}

	public String i_add() throws Exception {
		HttpSession session = request.getSession();
		if (image != null) {
			fileUpload("images/lost", image);
		} else {
			session.setAttribute("lost_add_msg", "*文件上传失败");
		}
		try {
			lost.setUid(uid);
			lost.setImgurl("images/lost/" + imageFileName);
			if (lostService.insert(lost)) {
				systemService.refreshApplication(ActionContext.getContext().getApplication());
				session.setAttribute("lost_add_msg", "*添加失物成功");
			} else {
				session.setAttribute("lost_add_msg", "*添加失物失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "lostadd";
	}

	@SuppressWarnings("unchecked")
	public String show_user_lost_list() throws Exception {
		HttpSession session = request.getSession();
		if (uid > 0) {
			Lost lost = new Lost();
			lost.setUid(uid);
			List<Lost> user_lost_list = (List<Lost>) lostService.getAllLost(1, 10, lost).get("rows");
			if (user_lost_list != null) {
				session.setAttribute("user_lost_list", user_lost_list);
				return "showUserLost";
			} else {
				session.setAttribute("user_lost_list", null);
				return "showUserLost";
			}
		}
		return "showUserLost";
	}

	public void update() throws Exception {
		Json j = new Json();
		fileUpload("images/lost", image);
		lost.setImgurl("images/lost/" + imageFileName);
		if (lostService.update(lost)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
	}

	public String i_update_show() throws Exception {
		HttpSession session = request.getSession();
		Lost lo = lostService.getLostById(lid);
		if (lo != null) {
			session.setAttribute("update_lost", lo);
		} else {
			session.setAttribute("update_lost", null);
		}
		return "lo-update";
	}

	public String i_update() throws Exception {
		HttpSession session = request.getSession();
		if (imageFileName != null) {
			fileUpload("images/lost", image);
			lost.setImgurl("images/lost/" + imageFileName);
		} else {
			lost.setImgurl(lostService.getLostById(lid).getImgurl());
		}
		lost.setUid(uid);
		lost.setLid(lid);
		if (lostService.update(lost)) {
			session.setAttribute("user_lost_update_msg", "* 修改成功");
		} else {
			session.setAttribute("user_lost_update_msg", "* 修改失败");
		}
		return "lo-update";
	}

	public void delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			Lost c = lostService.getLostById(Integer.parseInt(idsStr[i]));
			if (c != null) {
				lostService.delete(c.getLid());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
	}

	@SuppressWarnings("unchecked")
	public String i_delete() throws Exception {
		Lost c = lostService.getLostById(lid);
		if (c != null) {
			lostService.delete(c.getLid());
		}
		HttpSession session = request.getSession();
		if (uid > 0) {
			Lost lost = new Lost();
			lost.setUid(uid);
			List<Lost> user_lost_list = (List<Lost>) lostService.getAllLost(1, 10, lost).get("rows");
			if (user_lost_list != null) {
				session.setAttribute("user_lost_list", user_lost_list);
				return "showUserLost";
			} else {
				session.setAttribute("user_lost_list", null);
				return "showUserLost";
			}
		}
		return "showUserLost";
	}

	public void fileUpload(String path, File inputFile) throws IOException {
		String realpath = ServletActionContext.getServletContext().getRealPath(path);
		System.out.println(realpath);
		if (inputFile != null) {
			File savefile = new File(new File(realpath), imageFileName);
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			FileUtils.copyFile(inputFile, savefile);
		}
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Lost getLost() {
		return lost;
	}

	public void setLost(Lost lost) {
		this.lost = lost;
	}

	public LostServiceI getLostService() {
		return lostService;
	}

	@Autowired
	public void setLostService(LostServiceI lostService) {
		this.lostService = lostService;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	private int uid;

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	private int lid;

	public int getLid() {
		return lid;
	}

	public void setLid(int lid) {
		this.lid = lid;
	}

	public SystemServiceI getSystemService() {
		return systemService;
	}

	@Autowired
	public void setSystemService(SystemServiceI systemService) {
		this.systemService = systemService;
	}

}
